﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;
using WebBanThuoc.Function;

namespace WebBanThuoc.admin
{
    public partial class QLCauHoi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }



            if (!IsPostBack)
            {
                if (Request.QueryString["MaBaiDang"] != null)
                {
                    getData();
                }
                
            }
            else
            {
                if (checkDefaultSubmit())
                {
                    getData();
                }
                else
                {
                    //if (Request.Params["btn_Them"] != null)
                    //{
                    //    addProduct();
                    //}
                    if (Request.Params["btn_Sua"] != null)
                    {
                        updateProduct();
                    }
                    if (Request.Params["btn_Xoa"] != null)
                    {
                        deleteProduct();
                    }
                }
            }
        }

        private void getData()
        {
            string MaBaiDang = Request.QueryString["MaBaiDang"];
            if (MaBaiDang == null)
            {
                MaBaiDang = Request.Params["MaBaiDang"];
            }
            if (MaBaiDang != null && MaBaiDang.Trim() != "")
            {
                IServerWeb sw = new ServerWeb();
                BaiDang_DAO dao = new BaiDang_DAO(sw.Connection);
                BaiDang data = dao.getByKey(int.Parse(Func.beginConvertDataType(MaBaiDang, pDataType.Integer)));
                sw.closeConnection();
                if (data != null)
                {
                    Session.Add("page_QLCauHoi_DataCH", data);

                    NoiDung.Text = data.NoiDung;
                    TraLoi.Text = data.TraLoi;

                }
                else
                {
                    NoiDung.Text = "";
                    TraLoi.Text = "";
                }
            }
        }


        private bool checkDefaultSubmit()
        {
            if (Request.Params["btn_Xoa"] != null || Request.Params["btn_Sua"] != null)
            {
                return false;
            }
            return true;
        }

        private void deleteProduct()
        {
            string MaBaiDang = Request.Params["MaBaiDang"];
            if (MaBaiDang != null || MaBaiDang.ToString().Trim() != "")
            {

                IServerWeb sw = new ServerWeb();
                BaiDang_DAO dao = new BaiDang_DAO(sw.Connection);

                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = dao.deleteByKey(int.Parse(Func.beginConvertDataType(MaBaiDang, pDataType.Integer)));
                if (tempRS > 0)
                {
                    Session.Add("page_QLCauHoi_msgSucessFunc", "Đã Xóa thành công Câu hỏi có mã " + MaBaiDang);
                    NoiDung.Text = "";
                    TraLoi.Text = "";
                }
                sw.closeConnection();
            }
        }

        private void updateProduct()
        {
            string MaBaiDang = Request.Params["MaBaiDang"];
            if (MaBaiDang != null || MaBaiDang.ToString().Trim() != "")
            {

                IServerWeb sw = new ServerWeb();
                BaiDang_DAO dao = new BaiDang_DAO(sw.Connection);
                //SanPham sp = daoSP.getByKey("SP" + Request.Params["MaSanPham"].ToString().Trim());
                BaiDang data = dao.getByKey(int.Parse(Func.beginConvertDataType(MaBaiDang, pDataType.Integer)));

                //data.TraLoi = Request.Params["TraLoi"].ToString();
                data.TraLoi = TraLoi.Text;

                data.KiemDuyet = Request.Params["KiemDuyet"] == null ? false : true;                
                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = dao.updateByKey(data, data.MaBaiDang);
                //tempRS = 1;
                if (tempRS > 0)
                {
                    BaiDang spNew = dao.getByKey(data.MaBaiDang);
                    Session.Add("page_QLCauHoi_msgSucessFunc", "Đã Cập nhật thành công câu hỏi " + spNew.MaBaiDang);
                    Session.Add("page_QLCauHoi_DataCH", spNew);
                    NoiDung.Text = spNew.NoiDung;
                    TraLoi.Text = spNew.TraLoi;
                }
                sw.closeConnection();
            }
        }

    }
}